
#include "game.h"

// 初始化棋盘
void Initializeboard(char arr[rows][cols], int r, int c, char set)
{
    int i = 0;
    for (i = 0; i < rows; i++)
    {
        int j = 0;
        for (j = 0; j < cols; j++)
        {
            arr[i][j] = set;
        }
    }
}

// 打印棋盘
void Displayboard(char arr[rows][cols], int r, int c)
{
    int i = 0;
    for (i = 0; i <= row; i++)
    {
        printf("%d ", i);
    }
    printf("\n");
    for (i = 1; i <= row; i++)
    {
        printf("%d ", i);
        int j = 0;
        for (j = 1; j <= col; j++)
        {
            printf("%c ", arr[i][j]);
        }
        printf("\n");
    }
    printf("--------------------\n");
}

// 布置雷2
void Setmine(char arr[rows][cols], int r, int c)
{
    int count = num_mine;
    int i = 0;
    int j = 0;
    while (count)
    {
        int x = rand() % row + 1;
        int y = rand() % col + 1;
        if (arr[x][y] == '0')
        {
            arr[x][y] = '1';
            count--;
        }
    }
}

// 统计排查位置周围雷的个数
int sum_arround(char arr1[rows][cols], int r, int c, int x, int y)
{
    int sum = 0;
    int i = 0;
    int j = 0;

    for (i = x - 1; i <= x + 1; i++)
    {
        for (j = y - 1; j <= y + 1; j++)
        {
            if (arr1[i][j] == '1')
            {
                sum++;
            }
        }
    }
    return sum;
}

// 排查雷
void Findmine(char arr1[rows][cols], char arr2[rows][cols], int r, int c)
{
    int excludemine = 0;
    int x = 0;
    int y = 0;
    while (excludemine < (row * col - num_mine))
    {
        printf("请输入想排查的坐标(行,列):\n");
        scanf("%d %d", &x, &y);
        if ((x >= 1 && x <= row) && (y >= 1 && y <= col))
        {
            if (arr1[x][y] == '1')
            {
                printf("很遗憾,你被炸死了\n");
                printf("炸弹分布如下:\n");
                Displayboard(arr1, rows, cols);
                break;
            }
            else
            {
                arr2[x][y] = sum_arround(arr1, rows, cols, x, y) + '0';
                Displayboard(arr2, rows, cols);
            }
        }
        else
        {
            printf("输入的坐标非法,请重新输入.\n");
        }
        excludemine++;
    }
    if (excludemine == (row * col - num_mine))
    {
        printf("恭喜你,扫雷成功\n");
        Displayboard(arr1, rows, cols);
    }
}
